define(function (require) {
    require('share');
    var utils = require('utils'), boxId = utils.getUrlParam('id'), socketActive = false, websocket,
        socketUrl = 'ws://259w758b32.zicp.vip/';
    var viewCode = 500, closeCode = 1000, takeCode = 1500, bindCode = 2000;
    var sendMsg = {
        'boxId': boxId,
        'type': viewCode,
        'code': 1000,
        'data': ''
    };
    var obj = {
        init: function () {
            this.getData()
        },
        getData: function () {
            if (!boxId) {
                utils.goBack()
            }
            var index = utils.tipsLoading("数据加载中");
            var rest = $.restPost('boxGroup/getBoxInfo', {
                boxId: boxId,
                userId: JSON.parse(utils.getLocalStorage('userinfo')).id
            });
            rest.done(function (msg, data) {
                utils.tipsClose(index);
                $('#cabinet-number').text(data.name);
                !socketActive && obj.connect();
                if (data.certificateVOs.length === 0) {
                    $('.empty,#box-bind').removeClass('hide');
                    $('#box-take').addClass('hide');
                } else {
                    $('#box-take').removeClass('hide');
                    $('#box-bind').addClass('hide');
                }
                console.log(data)
            });
            rest.fail(function (msg) {
                utils.tipsClose(index);
                utils.tipsError(msg);
            });
        },
        connect: function () {
            websocket = new WebSocket(`${socketUrl}${boxId}/msg.ws`);
            websocket.onopen = function (evt) {
                socketActive = true;
                console.log('通讯建立');
            };
            websocket.onclose = function (evt) {
                socketActive = false;
                console.log('通讯关闭');
                obj.connect();
            };
            websocket.onmessage = function (evt) {
                var msg = JSON.parse(evt.data);
                utils.tipsClose();
                if (msg.code === 1000) {
                    console.log('********socket信息*********')
                    console.log(msg)
                    console.log('********socket信息*********')
                    utils.tipsError(msg.message)
                    switch (msg.type) {
                        case viewCode:
                            utils.tipsError(msg.message);
                            break;
                        case closeCode:
                            utils.tipsError(msg.message);
                            obj.getData()
                            break;
                        case takeCode:
                            utils.tipsError(msg.message);
                            break;
                        case bindCode:
                            utils.tipsError(msg.message);
                            break;
                    }
                } else {
                    console.log('********socket信息【ERROR】*********')
                    console.log(msg)
                    console.log('********socket信息【ERROR】*********')
                    utils.tipsError(msg.message)
                }
            };
            websocket.onerror = function (evt) {
                utils.tipsError(evt)
            };
        },
        viewBox: function () {
            if (socketActive) {
                var index = utils.tipsLoading("柜门打开中，请稍后");
                try {
                    websocket.send(JSON.stringify(sendMsg));
                } catch (e) {
                    utils.tipsClose(index);
                    console.log(e)
                }
            } else {
                utils.tipsError('socket未连接')
            }
        },
        scan: function () {
            window.OpenScan.open()
        },
        getDataByNumber: function () {
			var scanInfo = $(this).data('res');
            if (!scanInfo) {
                return
            }
            var index = utils.tipsLoading("数据加载中");
            var rest = $.restPost('boxGroup/scan', {
                boxId: boxId,
                scanInfo: scanInfo,
                userId: JSON.parse(utils.getLocalStorage('userinfo')).id
            });
            rest.done(function (msg, data) {
                utils.tipsClose(index);
                console.log(data);
                utils.tipsSingleSelect('数据信息', ['确认放证', '取消'], function () {
                    obj.bindData(scanInfo)
                })
            });
            rest.fail(function (msg) {
                utils.tipsClose(index);
                utils.tipsError(msg);
            });
        },
        bindData: function (scanInfo) {
            if (socketActive) {
                var index = utils.tipsLoading("柜门打开中，请稍后");
                try {
                    sendMsg.type = bindCode;
                    sendMsg.data = scanInfo;
                    websocket.send(JSON.stringify(sendMsg));
                } catch (e) {
                    utils.tipsClose(index);
                    console.log(e)
                }
            } else {
                utils.tipsError('socket未连接')
            }
        },
        takeCert: function () {
            if (socketActive) {
                var index = utils.tipsLoading("柜门打开中，请稍后");
                try {
                    sendMsg.type = takeCode;
                    sendMsg.data = '';
                    websocket.send(JSON.stringify(sendMsg));
                } catch (e) {
                    utils.tipsClose(index);
                    console.log(e)
                }
            } else {
                utils.tipsError('socket未连接')
            }
        }
    };

    $(function () {
        obj.init();
        $('#box-view').click(obj.viewBox);
        $('#box-bind').click(obj.scan)
        $('#box-take').click(obj.takeCert);
        $('#trigger').click(obj.getDataByNumber)
    })

});